package com.woniu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.project.client.ProductEntityCient;
import com.woniu.project.core.domain.*;
import com.woniu.project.core.domain.ProductProperty;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dao.ProductPropertyMapper;
import com.woniu.project.dto.productproperty.ProdcutPropertySelectDto;
import com.woniu.project.params.productproperty.ProdcutPropertyAddParam;
import com.woniu.project.params.productproperty.ProdcutPropertySelectParam;
import com.woniu.project.params.productproperty.ProdcutPropertyUpdateParam;
import com.woniu.project.service.IProductEntityPropertyCentreService;
import com.woniu.project.service.IProductPropertyCentreService;
import com.woniu.project.service.IProductPropertyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.project.service.IProductPropertyValueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@Service
@Transactional
public class ProductPropertyServiceImpl extends ServiceImpl<ProductPropertyMapper, ProductProperty> implements IProductPropertyService {
    @Autowired
    private IProductPropertyCentreService productPropertyCentreService;
    @Autowired
    private IProductPropertyValueService productPropertyValueService;
    @Autowired
    private IProductEntityPropertyCentreService productEntityPropertyCentreService;
    @Autowired
    private ProductEntityCient productEntityCient;
    //根据是否有id和name进行不同的分页查询
    @Override
    public IPage<ProdcutPropertySelectDto> select(ProdcutPropertySelectParam prodcutPropertySelectParam) {
        Page<ProductProperty> page = new Page<>(prodcutPropertySelectParam.getPage(), prodcutPropertySelectParam.getSize());
        QueryWrapper<ProductProperty> wrapper = new QueryWrapper<ProductProperty>();
        //根据productId查询ProductProperty
        if (prodcutPropertySelectParam.getProductId() != null && !prodcutPropertySelectParam.getProductId().equals("")) {
            QueryWrapper<ProductPropertyCentre> wrapper1 = new QueryWrapper<ProductPropertyCentre>();
            wrapper1.eq("product_id", prodcutPropertySelectParam.getProductId());
            List<ProductPropertyCentre> list = productPropertyCentreService.list(wrapper1);
            List<Integer> productPropertyIds = new ArrayList<>();
            for (ProductPropertyCentre productPropertyCentre : list) {
                productPropertyIds.add(productPropertyCentre.getProductPropertyId());
            }
            wrapper.in("product_property_id", productPropertyIds);
        }

        wrapper.eq(prodcutPropertySelectParam.getId() != null && !prodcutPropertySelectParam.getId().equals(""),
                "product_property_id", prodcutPropertySelectParam.getId());

        wrapper.like(prodcutPropertySelectParam.getName() != null && !prodcutPropertySelectParam.getName().equals(""),
                "product_property_name", prodcutPropertySelectParam.getName());

        wrapper.orderByAsc("product_property_sort");
        IPage ipage = page(page, wrapper);
        //entity换为dto
        List<ProductProperty> records = ipage.getRecords();
        List<ProdcutPropertySelectDto> prodcutPropertySelectDtos = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            ProdcutPropertySelectDto prodcutPropertySelectDto = new ProdcutPropertySelectDto();
            BeanUtils.copyProperties(records.get(i), prodcutPropertySelectDto);
            Integer productPropertyId = records.get(i).getProductPropertyId();
            QueryWrapper<ProductPropertyValue> wrapper2 = new QueryWrapper<ProductPropertyValue>();
            wrapper2.eq("product_property_id",productPropertyId);
            List<ProductPropertyValue> productPropertyValues = productPropertyValueService.list(wrapper2);
            prodcutPropertySelectDto.setProductPropertyValues(productPropertyValues);
            prodcutPropertySelectDtos.add(prodcutPropertySelectDto);
        }
        ipage.setRecords(prodcutPropertySelectDtos);
        return ipage;
    }

    //根据商品实体id查询商品属性
    @Override
    public List<ProdcutPropertySelectDto> selectByEntityId(Integer id) {
        QueryWrapper<ProductEntityPropertyCentre> wrapper = new QueryWrapper<ProductEntityPropertyCentre>();
        //根据productEntityId查询ProductPropertyId和ProductPropertyValueId
        wrapper.eq("product_entity_id", id);
        List<ProductEntityPropertyCentre> centres = productEntityPropertyCentreService.list(wrapper);
        //查询对应的ProductProperty,并将entity换为dto
        List<ProductPropertyValue> productPropertyValues = new ArrayList<>();
        List<ProdcutPropertySelectDto> prodcutPropertySelectDtos = new ArrayList<>();
        for (ProductEntityPropertyCentre centre : centres) {
            ProdcutPropertySelectDto prodcutPropertySelectDto = new ProdcutPropertySelectDto();
            productPropertyValues.add(productPropertyValueService.getById(centre.getProductPropertyValueId()));
            BeanUtils.copyProperties(getById(centre.getProductPropertyId()), prodcutPropertySelectDto);
            prodcutPropertySelectDto.setProductPropertyValues(productPropertyValues);
            prodcutPropertySelectDtos.add(prodcutPropertySelectDto);
        }
        return prodcutPropertySelectDtos;
    }
    //新增商品属性
    @Override
    public int addProperty(ProdcutPropertyAddParam prodcutPropertyAddParam) {
        ProductProperty productProperty = new ProductProperty();
        BeanUtils.copyProperties(prodcutPropertyAddParam, productProperty);
        //查询所有大类序号，如有序号等于当前序号则返回1
        QueryWrapper<ProductProperty> wrapper = new QueryWrapper<ProductProperty>();
        wrapper.eq("product_property_sort", productProperty.getProductPropertySort());
        List<ProductProperty> list = list(wrapper);
        if (list.size() != 0) {
            return 1;
        }
        //属性增加
        save(productProperty);
        //属性值表增加,属性属性值中间表增加
        for (ProductPropertyValue productPropertyValue : prodcutPropertyAddParam.getProductPropertyValues()) {
            productPropertyValue.setProductPropertyId(productProperty.getProductPropertyId());
            productPropertyValueService.save(productPropertyValue);
            ProductPropertyCentre productPropertyCentre=new ProductPropertyCentre();
            productPropertyCentre.setProductId(prodcutPropertyAddParam.getProductId());
            productPropertyCentre.setProductPropertyId(productProperty.getProductPropertyId());
            productPropertyCentre.setProductPropertyValueId(productPropertyValue.getProductPropertyValueId());
            productPropertyCentreService.save(productPropertyCentre);
        }
        return 0;
    }
    //修改商品属性
    @Override
    public int updateProperty(ProdcutPropertyUpdateParam prodcutPropertyUpdateParam) {
        ProductProperty productProperty = new ProductProperty();
        BeanUtils.copyProperties(prodcutPropertyUpdateParam, productProperty);
        //查询所有大类序号，如有序号等于当前序号则返回1
        QueryWrapper<ProductProperty> wrapper = new QueryWrapper<ProductProperty>();
        wrapper.eq("product_property_sort", productProperty.getProductPropertySort());
        wrapper.ne("product_property_id", prodcutPropertyUpdateParam.getProductPropertyId());
        List<ProductProperty> list = list(wrapper);
        if (list.size() > 0) {
            return 1;
        }
        //属性修改
        updateById(productProperty);
        //属性值表修改,属性属性值中间表修改
        QueryWrapper<ProductPropertyCentre> wrapper1 = new QueryWrapper<ProductPropertyCentre>();
        QueryWrapper<ProductPropertyValue> wrapper2 = new QueryWrapper<ProductPropertyValue>();
        wrapper1.eq("product_property_id", productProperty.getProductPropertyId());
        wrapper2.eq("product_property_id", productProperty.getProductPropertyId());
        productPropertyCentreService.remove(wrapper1);
        productPropertyValueService.remove(wrapper2);
        for (ProductPropertyValue productPropertyValue : prodcutPropertyUpdateParam.getProductPropertyValues()) {
            productPropertyValue.setProductPropertyId(productProperty.getProductPropertyId());
            productPropertyValueService.save(productPropertyValue);
            ProductPropertyCentre productPropertyCentre=new ProductPropertyCentre();
            productPropertyCentre.setProductId(prodcutPropertyUpdateParam.getProductId());
            productPropertyCentre.setProductPropertyId(productProperty.getProductPropertyId());
            productPropertyCentre.setProductPropertyValueId(productPropertyValue.getProductPropertyValueId());
            productPropertyCentreService.save(productPropertyCentre);
        }
        return 0;
    }
    //根据id删除属性
    @Override
    public void deleteById(Integer id) {
        //根据属性id查询商品实体
        ResultDto resultDto = productEntityCient.selectByPropertyId(id);
        System.out.println("--------->>>>>>>>>>>>>>>"+resultDto.getData().toString());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!resultDto.getData().toString().equals("[]")){
            System.out.println("------------------");
            throw new ProjectProductException(500, "该属性还有对应商品，不能删除");
        }
        System.out.println(">>>>>>>>>>>>>>>>>>>>");
        //删除属性
        removeById(id);
        //删除属性与属性值中间表
        QueryWrapper<ProductPropertyCentre> wrapper=new QueryWrapper();
        wrapper.eq("product_property_id",id);
        productPropertyCentreService.remove(wrapper);
    }


}
